﻿
using System.Collections.Generic;
using Microsoft.Synchronization.Data.Server;

namespace Syncronization.Service.UnitTests.Model
{
    public partial class SimpleSyncSchemaController : SyncController
    {
        public SimpleSyncSchemaController()
        {
            Adapters = new List<SyncAdapter>();
            Adapters.Add(new UserSyncAdapter());
        }
    }


    public partial class UserSyncAdapter : SyncAdapter
    {
        public UserSyncAdapter()
        {
            this.InitializeCommands();
            this.InitializeAdapterProperties();
        }

        private void InitializeCommands()
        {
            // UserSyncTableInsertCommand command.
            InitializeInsertCommand();
            // UserSyncTableDeleteCommand command.
            InitializeDeleteCommand();
            // UserSyncTableUpdateCommand command.
            InitialiseUpdateCommand();
            // UserSyncTableSelectConflictDeletedRowsCommand command.
            InitializeDeleteRowsCommand();
            // UserSyncTableSelectConflictUpdatedRowsCommand command.
            InitializeUpdatedRowsCommand();
            // UserSyncTableSelectIncrementalInsertsCommand command.
            InitialzeIncrementalInsertCommand();
            // UserSyncTableSelectIncrementalDeletesCommand command.
            InitializeDeletesCommand();
            // UserSyncTableSelectIncrementalUpdatesCommand command.
            InitializeSelectIncrementalUpdatesCommand();
        }

        private void InitializeSelectIncrementalUpdatesCommand()
        {
            //TODO:Take out hardcode
            this.SelectIncrementalUpdatesCommand = new System.Data.SqlClient.SqlCommand();
            this.SelectIncrementalUpdatesCommand.CommandText = @"SELECT [UserID], [UserName], [Password], [FirstName], [LastName], [CreationDate], [ModifiedDate] FROM dbo.[User] WHERE ([ModifiedDate] > @sync_last_received_anchor AND [ModifiedDate] <= @sync_new_received_anchor AND [CreationDate] <= @sync_last_received_anchor)";
            this.SelectIncrementalUpdatesCommand.CommandType = System.Data.CommandType.Text;
            this.SelectIncrementalUpdatesCommand.Parameters.Add(new System.Data.SqlClient.SqlParameter("@sync_last_received_anchor", System.Data.SqlDbType.DateTime));
            this.SelectIncrementalUpdatesCommand.Parameters.Add(new System.Data.SqlClient.SqlParameter("@sync_new_received_anchor", System.Data.SqlDbType.DateTime));
        }

        private void InitializeDeletesCommand()
        {
            //TODO:Take out hardcode

            this.SelectIncrementalDeletesCommand = new System.Data.SqlClient.SqlCommand();
            this.SelectIncrementalDeletesCommand.CommandText = "SELECT [UserID], [DeletionDate] FROM dbo.User_Tombstone WHERE (@sync_initialized " +
                    "= 1 AND [DeletionDate] > @sync_last_received_anchor AND [DeletionDate] <= @sync_" +
                    "new_received_anchor)";
            this.SelectIncrementalDeletesCommand.CommandType = System.Data.CommandType.Text;
            this.SelectIncrementalDeletesCommand.Parameters.Add(new System.Data.SqlClient.SqlParameter("@sync_initialized", System.Data.SqlDbType.Bit));
            this.SelectIncrementalDeletesCommand.Parameters.Add(new System.Data.SqlClient.SqlParameter("@sync_last_received_anchor", System.Data.SqlDbType.DateTime));
            this.SelectIncrementalDeletesCommand.Parameters.Add(new System.Data.SqlClient.SqlParameter("@sync_new_received_anchor", System.Data.SqlDbType.DateTime));
        }

        private void InitialzeIncrementalInsertCommand()
        {
            //TODO:Take out hardcode

            this.SelectIncrementalInsertsCommand = new System.Data.SqlClient.SqlCommand();
            this.SelectIncrementalInsertsCommand.CommandText = "SELECT [UserID], [UserName], [Password], [FirstName], [LastName], [CreationDate]," +
                    " [ModifiedDate] FROM dbo.[User] WHERE ([CreationDate] > @sync_last_received_anch" +
                    "or AND [CreationDate] <= @sync_new_received_anchor)";
            this.SelectIncrementalInsertsCommand.CommandType = System.Data.CommandType.Text;
            this.SelectIncrementalInsertsCommand.Parameters.Add(new System.Data.SqlClient.SqlParameter("@sync_last_received_anchor", System.Data.SqlDbType.DateTime));
            this.SelectIncrementalInsertsCommand.Parameters.Add(new System.Data.SqlClient.SqlParameter("@sync_new_received_anchor", System.Data.SqlDbType.DateTime));
        }

        private void InitializeUpdatedRowsCommand()
        {
            //TODO:Take out hardcode

            this.SelectConflictUpdatedRowsCommand = new System.Data.SqlClient.SqlCommand();
            this.SelectConflictUpdatedRowsCommand.CommandText = "SELECT [UserID], [UserName], [Password], [FirstName], [LastName], [CreationDate]," +
                    " [ModifiedDate] FROM dbo.[User] WHERE ([UserID] = @UserID)";
            this.SelectConflictUpdatedRowsCommand.CommandType = System.Data.CommandType.Text;
            this.SelectConflictUpdatedRowsCommand.Parameters.Add(new System.Data.SqlClient.SqlParameter("@UserID", System.Data.SqlDbType.Int));
        }

        private void InitializeDeleteRowsCommand()
        {
            //TODO:Take out hardcode
            this.SelectConflictDeletedRowsCommand = new System.Data.SqlClient.SqlCommand();
            this.SelectConflictDeletedRowsCommand.CommandText = "SELECT [UserID], [DeletionDate] FROM dbo.User_Tombstone WHERE ([UserID] = @UserID" +
                    ")";
            this.SelectConflictDeletedRowsCommand.CommandType = System.Data.CommandType.Text;
            this.SelectConflictDeletedRowsCommand.Parameters.Add(new System.Data.SqlClient.SqlParameter("@UserID", System.Data.SqlDbType.Int));
        }

        private void InitialiseUpdateCommand()
        {
            //TODO:Take out hardcode
            this.UpdateCommand = new System.Data.SqlClient.SqlCommand();
            this.UpdateCommand.CommandText = @"UPDATE dbo.[User] SET [UserName] = @UserName, [Password] = @Password, [FirstName] = @FirstName, [LastName] = @LastName, [CreationDate] = @CreationDate, [ModifiedDate] = @ModifiedDate WHERE ([UserID] = @UserID) AND (@sync_force_write = 1 OR ([ModifiedDate] <= @sync_last_received_anchor)) SET @sync_row_count = @@rowcount";
            this.UpdateCommand.CommandType = System.Data.CommandType.Text;
            this.UpdateCommand.Parameters.Add(new System.Data.SqlClient.SqlParameter("@UserName", System.Data.SqlDbType.NVarChar));
            this.UpdateCommand.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Password", System.Data.SqlDbType.NVarChar));
            this.UpdateCommand.Parameters.Add(new System.Data.SqlClient.SqlParameter("@FirstName", System.Data.SqlDbType.NVarChar));
            this.UpdateCommand.Parameters.Add(new System.Data.SqlClient.SqlParameter("@LastName", System.Data.SqlDbType.NVarChar));
            this.UpdateCommand.Parameters.Add(new System.Data.SqlClient.SqlParameter("@CreationDate", System.Data.SqlDbType.DateTime));
            this.UpdateCommand.Parameters.Add(new System.Data.SqlClient.SqlParameter("@ModifiedDate", System.Data.SqlDbType.DateTime));
            this.UpdateCommand.Parameters.Add(new System.Data.SqlClient.SqlParameter("@UserID", System.Data.SqlDbType.Int));
            this.UpdateCommand.Parameters.Add(new System.Data.SqlClient.SqlParameter("@sync_force_write", System.Data.SqlDbType.Bit));
            this.UpdateCommand.Parameters.Add(new System.Data.SqlClient.SqlParameter("@sync_last_received_anchor", System.Data.SqlDbType.DateTime));
            System.Data.SqlClient.SqlParameter updatecommand_sync_row_countParameter = new System.Data.SqlClient.SqlParameter("@sync_row_count", System.Data.SqlDbType.Int);
            updatecommand_sync_row_countParameter.Direction = System.Data.ParameterDirection.Output;
            this.UpdateCommand.Parameters.Add(updatecommand_sync_row_countParameter);
        }

        private void InitializeDeleteCommand()
        {
            //TODO:Take out hardcode
            this.DeleteCommand = new System.Data.SqlClient.SqlCommand();
            this.DeleteCommand.CommandText = "DELETE FROM dbo.[User] WHERE ([UserID] = @UserID) AND (@sync_force_write = 1 OR (" +
                    "[ModifiedDate] <= @sync_last_received_anchor)) SET @sync_row_count = @@rowcount";
            this.DeleteCommand.CommandType = System.Data.CommandType.Text;
            this.DeleteCommand.Parameters.Add(new System.Data.SqlClient.SqlParameter("@UserID", System.Data.SqlDbType.Int));
            this.DeleteCommand.Parameters.Add(new System.Data.SqlClient.SqlParameter("@sync_force_write", System.Data.SqlDbType.Bit));
            this.DeleteCommand.Parameters.Add(new System.Data.SqlClient.SqlParameter("@sync_last_received_anchor", System.Data.SqlDbType.DateTime));
            System.Data.SqlClient.SqlParameter deletecommand_sync_row_countParameter = new System.Data.SqlClient.SqlParameter("@sync_row_count", System.Data.SqlDbType.Int);
            deletecommand_sync_row_countParameter.Direction = System.Data.ParameterDirection.Output;
            this.DeleteCommand.Parameters.Add(deletecommand_sync_row_countParameter);
        }

        private void InitializeInsertCommand()
        {
            //TODO:Take out hardcode
            this.InsertCommand = new System.Data.SqlClient.SqlCommand();
            this.InsertCommand.CommandText = @" SET IDENTITY_INSERT dbo.[User] ON INSERT INTO dbo.[User] ([UserID], [UserName], [Password], [FirstName], [LastName], [CreationDate], [ModifiedDate]) VALUES (@UserID, @UserName, @Password, @FirstName, @LastName, @CreationDate, @ModifiedDate) SET @sync_row_count = @@rowcount SET IDENTITY_INSERT dbo.[User] OFF ";
            this.InsertCommand.CommandType = System.Data.CommandType.Text;
            this.InsertCommand.Parameters.Add(new System.Data.SqlClient.SqlParameter("@UserID", System.Data.SqlDbType.Int));
            this.InsertCommand.Parameters.Add(new System.Data.SqlClient.SqlParameter("@UserName", System.Data.SqlDbType.NVarChar));
            this.InsertCommand.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Password", System.Data.SqlDbType.NVarChar));
            this.InsertCommand.Parameters.Add(new System.Data.SqlClient.SqlParameter("@FirstName", System.Data.SqlDbType.NVarChar));
            this.InsertCommand.Parameters.Add(new System.Data.SqlClient.SqlParameter("@LastName", System.Data.SqlDbType.NVarChar));
            this.InsertCommand.Parameters.Add(new System.Data.SqlClient.SqlParameter("@CreationDate", System.Data.SqlDbType.DateTime));
            this.InsertCommand.Parameters.Add(new System.Data.SqlClient.SqlParameter("@ModifiedDate", System.Data.SqlDbType.DateTime));
            System.Data.SqlClient.SqlParameter insertcommand_sync_row_countParameter = new System.Data.SqlClient.SqlParameter("@sync_row_count", System.Data.SqlDbType.Int);
            insertcommand_sync_row_countParameter.Direction = System.Data.ParameterDirection.Output;
            this.InsertCommand.Parameters.Add(insertcommand_sync_row_countParameter);
        }

        private void InitializeAdapterProperties()
        {
            this.TableName = "User";
        }
    }
}


